<h1>View Distributors<small> - Total Stores </small><small><?php echo count($distributors);?></small></h1>
<br/>
<div class="btn-group">
    <p><a href="add" class="btn btn-success">Add new store</a> <a href="list" class="btn btn-default">List View</a> <a href="map" class="btn btn-default">Map View</a> </p>
    <p>&nbsp;</p>
</div>

<div style="margin-top: -30px;">
    <form class="navbar-form navbar-left form-inline" role="search" action="/distributors/search" method="post">
        <div class="form-group" style="width: 300px;">
          <input type="text" class="form-control" name="q" id="cardsearchinput" placeholder="Search store name, suburb or postcode" value="<?php if(isset($_POST['q'])) echo $_POST['q']; ?>">
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary btn-sm">Quick Search</button>
        </div>
    </form>
</div>
</div>


<div class="btn-group pull-right">
        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="#">
          Export
          <span class="caret"></span>
        </button>
        <ul class="dropdown-menu">
          <li><a id="export-excel" href="#">Excel</a></li>
          <li><a id="export-pdf" href="#">PDF</a></li>
          <li><a id="export-csv" href="#">CSV</a></li>
        </ul>
</div></h1>
<script type="text/javascript">
    $(window).ready(function(){
        $('#export-csv').on('click', function(){
            $('#export').val(1);
            $('form').submit();
        });
    });
</script>
<div class="row">
  <form class="form-inline" style="margin: 0;" method="POST" action="" role="form">
    <div class="col-lg-12">
      <div class="form-group">
        <select name="search[status]" class="input-sm form-control" id="status">
          
          <option <?php if ((int)$search['status'] == 1){ echo 'selected="selected"';}?> value="1">Active</option>
          <option <?php if ((int)$search['status'] == 2){ echo 'selected="selected"';}?> value="2">Inactive</option>
          <option <?php if ((int)$search['status'] == 3){ echo 'selected="selected"';}?> value="3">Pending</option>
        </select>
      </div>
      <div class="form-group">
        <select name="search[company_id]" class="input-sm form-control" id="company_id">
          
          <option value="1" <?php if ((int)$search['company_id'] == 1){ echo 'selected="selected"';}?>>MedicalVitaDiet</option>
          <option value="2" <?php if ((int)$search['company_id'] == 2){ echo 'selected="selected"';}?>>BettyBaxter</option>
          <option value="3" <?php if ((int)$search['company_id'] == 3){ echo 'selected="selected"';}?>>Both</option>
        </select>
      </div>
      <div class="form-group">
        <select name="search[states]" class="input-sm form-control" id="state">
          <option>State</option>
            <?php foreach ($states as $s){?>
              <option <?php if ((int)$search['states'] == $s->id){ echo 'selected="selected"';}?> value="<?php echo $s->id?>"><?php echo $s->title;?></option>
            <?php }?>
        </select>
      </div>
      <div class="form-group">
        <select name="search[regions]" class="input-sm form-control" id="region">
          <option>Region</option>
            <?php foreach ($regions as $r){?>
              <option <?php if ((int)$search['regions'] == $r->id){ echo 'selected="selected"';}?> value="<?php echo $r->id?>"><?php echo $r->title;?></option>
            <?php }?>
        </select>
      </div>
      
      <button id="search-distributors" type="submit" class="btn btn-primary btn-sm">Details Search</button>
      <br />
      <div style="margin-top: 10px;"></div>
      <div class="form-group">
        <select name="search[supplier]" class="input-sm form-control" id="advanced_search">
          <option value="0">Normal Search</option>
          <option value="1">Advanced Search</option>
        </select>
      </div>
      <br />
      <div style="margin-top: 10px;"></div>
      <div id="my_search" style="display: none;">
          <div class="form-group">
            <select name="search[supplier]" class="input-sm form-control" id="suppliers">
              <option>Supplier</option>
              <option value="0">No supplier</option>
                <?php foreach ($suppliers as $s){?>
                  <option <?php if ((int)$search['supplier'] == $s->id){ echo 'selected="selected"';}?> value="<?php echo $s->id?>"><?php echo $s->title;?></option>
                <?php }?>
            </select>
          </div>
          <div class="form-group">
            <select name="search[groups]" class="input-sm form-control" id="groups">
              <option>Groups</option>
              <option value="0">No group</option>
                <?php foreach ($groups as $g){?>
                  <option <?php if ((int)$search['groups'] == $g->id){ echo 'selected="selected"';}?> value="<?php echo $g->id?>"><?php echo $g->title;?></option>
                <?php }?>
            </select>
          </div>
      </div>
      
      
      
      <div class="form-group">
        <input class="form-group" type="hidden" id="export" name="search[export]" value="0"/>
        
      </div>
    </div>
  </form>
</div>

<div class=" table-responsive">
  <table class="table table-striped table-hover">
    <thead>
        <tr>
            <th width="42">Status</th>
            <th width="63">Company</th>
            <th width="30"><div align="center">API #</div></th>
            <th width="28"><div align="left">City</div></th>
            <th width="35"><div align="center">State</div></th>
            <th width="52"><div align="left">Contact</div></th>
            <th width="112"><div align="left">Phone</div></th>
            <th width="63"><div align="center">Supplier</div></th>
            <th width="67"><div align="center">Company Type</div></th>
            <th width="73"><div align="center"></div></th>
            <th width="147"></th>
        </tr>
    </thead>
    <tbody>
        <?php if ($distributors){?>
          <?php foreach ($distributors as $d){
            ?>
            <tr class=" card">
                <td>
                  <?php 
                  if(isset($d['status'])){
                      switch ($d['status']){
                        default:
                        case "1":
                          echo '<span class="label label-success">Active</span>';
                          break;
                        case "0":
                          echo '<span class="label label-important">Inactive</span>';
                          break;
                        case "2":
                          echo '<span class="label label-default">Pending</span>';
                          break;
                      }
                  }
                  ?>
                  
                </td>
                <td><?php if (isset($d['distributors_name'])) echo $d['distributors_name']?></td>
                <td><div align="center"><?php if (isset($d['api'])) echo $d['api']?></div></td>
                <td><div align="left"><?php if (isset($d['api'])) echo $d['api']?></div></td>
                <td><div align="center"><?php if (isset($d['state_id'])) echo $states[$d['state_id']]->title?></div></td>
                <td><div align="left"><?php if (isset($d['contact_name'])) echo $d['contact_name']?></div></td>
                <td><div align="left"><?php if (isset($d['phone'])) echo $d['phone']?></div></td>
                <td><div align="center">
                  <?php 
                  if(isset($d['supplier_id'])){
                    if ($d['supplier_id'] != 0) { echo $suppliers[$d['supplier_id']]->title; }else{ echo '-'; } 
                  }?>
                </div></td>
                <td>
                  <div align="center">
                    <?php 
                    if(isset($d['company_id'])){
                        switch ($d['company_id']){
                          default:
                          case '1':
                            echo "MedicalVitaDiet";
                            break;
                          case '2':
                            echo "BettyBaxter";
                            break;
                          case '3':
                            echo "Both";
                            break;
                        }
                    }
                  ?>
                <div class="btn-group">
                <td><div align="center"><a href="/distributors/view/<?php echo $d['distributors_id']?>" target="new">View Store</a>
                  </li>
                </div></td>
                <td>
                    <div class="btn-group">
                      <button class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="#">
                        Options
                        <span class="caret"></span>
                      </button>
                      <?php if(isset($d['distributors_id'])) {?>
                      <ul class="dropdown-menu">
                        <li><a href="/distributors/view/<?php echo $d['distributors_id']?>" target="new"><i class="icon-eye-open"></i>View Store</a></li>
                        <li><a href="/orders/add/<?php echo $d['distributors_id']?>" target="new"><i class="icon-shopping-cart"></i> Create Order</a></li>
                        <li><a href="/contact/create/<?php echo $d['distributors_id']?>" target="new"><i class="icon-pencil"></i> Add Contact log</a></li>
                        <li><a href="mailto:<?php echo $d['email']?>"><i class="icon-envelope"></i> E-Mail</a></li>
                        <li><a href="/distributors/edit/<?php echo $d['distributors_id']?>" target="new"><i class="icon-edit"></i> Edit</a></li>
                        <li><a href="/distributors/delete/<?php echo $d['distributors_id']?>"><i class="icon-trash"></i> Delete</a></li>
                      </ul>
                      <?php } ?>
                    </div>
                </td>
            </tr>
          <?php } ?>
        <?php }?>
    </tbody>
</table>
</div> 
 <script>
 $(document).ready(function() {
        var value = $('#advanced_search').val();
        if(value == 0){
            $('#my_search').hide();
        }
        
        $('#advanced_search').on('change', function() {
            if(this.value == 1){
                $('#my_search').show();
            }else{
                $('#my_search').hide();
            }
        })
});
 </script>  